﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;
using WebFormsMvp.Contrib.Views;
using WebFormsMvp.Contrib.Presenters;
using Ninject.Core;

namespace WebFormsMvp.Contrib.Ninject.Tests
{
    [TestClass]
    public class MvpPresenterKernelTests
    {
        [TestMethod]
        public void Instance_Of_Correct_Type()
        {
            //Arrange
            var view = MockRepository.GenerateMock<IAuthenticationView>();
            MvpPresenterKernel kernel = new TestingMvpPresenterKernel();
            
            var presenter = kernel.Create(typeof(AuthenticationPresenter), typeof(IAuthenticationView), view);
            //Act

            //Assert
            Assert.IsInstanceOfType(presenter, typeof(AuthenticationPresenter));
            Assert.AreSame(((AuthenticationPresenter)presenter).View, view);
        }

        [TestMethod]
        public void Multiple_Presenters_Are_Unique()
        {
            //Arrange
            var view = MockRepository.GenerateMock<IAuthenticationView>();
            var kernel = new TestingMvpPresenterKernel();

            var view2 = MockRepository.GenerateMock<IAuthenticationView>();

            var p1 = (CustomAuthenticationPresenter)kernel.Create(typeof(CustomAuthenticationPresenter), typeof(IAuthenticationView), view);
            var p2 = (CustomAuthenticationPresenter)kernel.Create(typeof(CustomAuthenticationPresenter), typeof(IAuthenticationView), view2);
            //Act

            //Assert
            Assert.AreNotSame(p1, p2);
        }

        [TestMethod]
        public void Presenters_Have_Unique_Views()
        {
            //Arrange
            var view = MockRepository.GenerateMock<IAuthenticationView>();
            var kernel = new TestingMvpPresenterKernel();

            var view2 = MockRepository.GenerateMock<IAuthenticationView>();

            var p1 = (CustomAuthenticationPresenter)kernel.Create(typeof(CustomAuthenticationPresenter), typeof(IAuthenticationView), view);
            var p2 = (CustomAuthenticationPresenter)kernel.Create(typeof(CustomAuthenticationPresenter), typeof(IAuthenticationView), view2);
            //Act

            //Assert
            Assert.AreNotSame(p1.View, p2.View);
        }

        [TestMethod]
        public void Preseters_Share_Service_Only()
        {
            //Arrange
            var view = MockRepository.GenerateMock<IAuthenticationView>();
            var kernel = new TestingMvpPresenterKernel();

            var view2 = MockRepository.GenerateMock<IAuthenticationView>();

            var p1 = (CustomAuthenticationPresenter)kernel.Create(typeof(CustomAuthenticationPresenter), typeof(IAuthenticationView), view);
            var p2 = (CustomAuthenticationPresenter)kernel.Create(typeof(CustomAuthenticationPresenter), typeof(IAuthenticationView), view2);
            //Act

            //Assert
            Assert.IsNotNull(p1.DataService);
            Assert.IsNotNull(p2.DataService);
            Assert.AreSame(p1.DataService, p2.DataService);
            Assert.AreNotSame(p1.View, p2.View);
        }
    }
}
